package com.corpgovernment.organization.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.validation.Valid;

import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.corpgovernment.api.organization.dto.request.BookingRelation;
import com.corpgovernment.api.organization.model.bookingrelation.*;
import com.corpgovernment.api.organization.model.org.GetOrgTreeResponse;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.CommonException;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.operatelog.OperateLog;
import com.corpgovernment.common.operatelog.constant.OperateLogContant;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.organization.entity.db.MbBookingRelation;
import com.corpgovernment.organization.service.BookingRelationService;
import com.corpgovernment.organization.util.LogUtil;
import com.corpgovernment.organization.vo.BookingRelationResponse;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 *  代订
 */
@RestController
@RequestMapping("/bookingRelation")
@Slf4j
public class BookingRelationController {

    @Autowired
    private BookingRelationService bookingRelationService;

    @PostMapping("/list")
    public JSONResult<ListBRResp> list(@RequestBody(required = false) ListBRReq req) {
        if (req == null) {
            req = new ListBRReq();
        }
        return JSONResult.success(bookingRelationService.pageQuery(req));
    }

    @PostMapping("/listBookedUser")
    public JSONResult<ListBookedUserResponse> listBookedUser(@RequestBody @Valid ListBookedUserRequest req) {
        return JSONResult.success(LogUtil.tryAndLog(sj -> bookingRelationService.listBookedUser(req, sj)));
    }

    @PostMapping("/bookingRange")
    public JSONResult<List<BookingRelationResponse>> listBookingRange(@RequestBody @Valid UidOrgIdEntry req) {
        return JSONResult.success(bookingRelationService.listBookingRange(req.getOrgId(), req.getUid()));
    }

    @PostMapping("/listBookingRangeOrgTree")
    public JSONResult<List<GetOrgTreeResponse>> listBookingRangeOrgTree(@RequestBody @Valid ListBookingRangeOrgTreeRequest req) {
        return JSONResult.success(bookingRelationService.listBookingRangeOrgTree(req.getOrgId(), req.getUid()));
    }

    @PostMapping("/checkExistences")
    public JSONResult<List<UidOrgIdEntry>> existence(@RequestBody @Valid CheckExistenceReq req) {
        return JSONResult.success(bookingRelationService.checkConfig(req));
    }

    @PostMapping("/add")
    @OperateLog(OperateLogContant.BOOKADD)
    public JSONResult<?> add(@RequestBody @Validated(UidOrgIdEntry.AddOrUpdateOnly.class) AddOrUpdateBRReq req) {
        bookingRelationService.batchInsert(req);
        return JSONResult.ok();
    }

    @PostMapping("/disable")
    @OperateLog(OperateLogContant.BOOKDISABLE)
    public JSONResult<?> disable(@RequestBody @Valid UidOrgIdEntry req) {
        bookingRelationService.updateStatus(req, MbBookingRelationVo.STATUS_DISABLE, Collections.singletonList(MbBookingRelationVo.STATUS_ENABLE));
        return JSONResult.ok();
    }

    @PostMapping("/enable")
    @OperateLog(OperateLogContant.BOOKENABLE)
    public JSONResult<?> enable(@RequestBody @Valid UidOrgIdEntry req) {
        bookingRelationService.updateStatus(req, MbBookingRelationVo.STATUS_ENABLE, Collections.singletonList(MbBookingRelationVo.STATUS_DISABLE));
        return JSONResult.ok();
    }

    @PostMapping("/allowAddForNonEmp")
    public JSONResult<?> allowAddForNonEmp(@RequestBody @Valid UidOrgIdEntry req) {
        bookingRelationService.setBookForNonEmployee(req, true);
        return JSONResult.ok();
    }

    @PostMapping("/prohibitAddForNonEmp")
    public JSONResult<?> prohibitAddForNonEmp(@RequestBody @Valid UidOrgIdEntry req) {
        bookingRelationService.setBookForNonEmployee(req, false);
        return JSONResult.ok();
    }

    @PostMapping("/delete")
    @OperateLog(OperateLogContant.BOOKDELETE)
    public JSONResult<?> delete(@RequestBody UidOrgIdEntry req) {
        if (StringUtils.isBlank(req.getUid())) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.UID_NULL);
        }
        bookingRelationService.updateStatus(req, MbBookingRelationVo.STATUS_DELETED, Arrays.asList(MbBookingRelationVo.STATUS_DISABLE, MbBookingRelationVo.STATUS_ENABLE));
        return JSONResult.ok();
    }

    /**
     * 查询前台用户某个角色下的代订范围
     *
     * @param req orgId 为用户隶属于的组织ID
     * @return 该用户未配置代订范围时，返回null
     */
    @PostMapping("/queryBookingRange")
    public JSONResult<BookingRange> queryBookingRange(@RequestBody @Valid UidOrgIdEntry req) {
        return JSONResult.success(bookingRelationService.queryBookingRangeByUidAndOrgId(req.getUid(), req.getOrgId()));
    }


    /**
     * 同步代订人
     * @param bookingRelation
     * @return
     */
    @PostMapping("/syncUpdate")
    public JSONResult syncBookingRelation(@RequestBody BookingRelation bookingRelation){
        bookingRelationService.syncBookingRelation(bookingRelation);
        return JSONResult.ok();
    }

    /**
     * 代订人查询
     * @param bookingEmployeeRequest
     * @return
     */
    @PostMapping("/queryBookingEmployee")
    public JSONResult queryBookingEmployee(@RequestBody BookingEmployeeRequest bookingEmployeeRequest, BaseUserInfo userInfo){
        bookingEmployeeRequest.setOrgId(userInfo.getOrgId());
        bookingEmployeeRequest.setUid(userInfo.getUid());
        return JSONResult.success(bookingRelationService.queryBookingEmployee(bookingEmployeeRequest));
    }

    @PostMapping("/queryBookingUsers")
    public JSONResult<List<BookingRelation.BookingUser>> queryBookingUsers (@RequestBody BookingEmployeeRequest bookingEmployeeRequest) {
        return JSONResult.success(bookingRelationService.queryBookingUsers(bookingEmployeeRequest.getUid(), bookingEmployeeRequest.getOrgId()));
    }

}
